<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/29 0029
 * Time: 22:04
 */

namespace console\controllers;

use app\models\ArcAndroid;
use app\models\ArcClassify;
use app\models\ArcProgress;
use app\models\ArcTrain;
use app\models\AsrAudio;
use app\models\AsrCustomer;
use app\models\AsrFiles;
use app\models\AsrKeyword;
use app\models\AsrKeywordClassify;
use app\models\AsrPlan;
use app\models\ThingRecordFile;
use common\helps\AsrSearch;
use common\helps\Foo;
use common\helps\ProcessStart;

//打开所有级别的错误
//error_reporting(-1);

class WebSocketsController extends CommonController
{

    public function actionStart($address, $port, $bufferLength=2048)
    {
        $echo = new echoServer($address,$port,$bufferLength);
        try {
            $echo->run();
        }
        catch (Exception $e) {
            $echo->stdout($e->getMessage());
        }
    }

}

require_once('./console/websocket/websockets.php');

class echoServer extends \WebSocketServer {

    function __construct($addr, $port, $bufferLength) {
        parent::__construct($addr, $port, $bufferLength);
        $this->userClass = 'MyUser';
    }

    //protected $maxBufferSize = 1048576; //1MB... overkill for an echo server, but potentially plausible for other applications.

    protected function process ($user, $message) {
        $this->send($user,$message);
    }

    protected function connected ($user) {
        // Do nothing: This is just an echo server, there's no need to track the user.
        // However, if we did care about the users, we would probably have a cookie to
        // parse at this step, would be looking them up in permanent storage, etc.
    }

    protected function closed ($user) {
        // Do nothing: This is where cleanup would go, in case the user had any sort of
        // open files or other objects associated with them.  This runs after the socket
        // has been closed, so there is no need to clean up the socket itself here.
    }
}